
# -*- coding: utf-8 -*-

from odoo import models, fields, api
from odoo.exceptions import UserError

class RejectWizard(models.TransientModel):
    """This wizard can be launched from an he.expense (an expense line)
    or from an hr.expense.sheet (En expense report)
    'hr_expense_refuse_model' must be passed in the context to differentiate
    the right model to use.
    """

    _name = "approval.request.reject.wizard"
    _description = "Expense Refuse Reason Wizard"

    reason = fields.Char(string='原因', required=True)
    request_id = fields.Many2one('approval.request')

    @api.model
    def default_get(self, fields):
        res = super(RejectWizard, self).default_get(fields)
        active_ids = self.env.context.get('active_ids', [])

        res.update({
            'request_id': active_ids[0] if active_ids else False,
        })
        return res

    def expense_refuse_reason(self):
        self.ensure_one()

        if self.request_id:
            self.request_id.request_status='refused'
            self.request_id.message_post(body=self.reason)

        return {'type': 'ir.actions.act_window_close'}
